package com.flyme.module.base.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysMessageSend;
import com.flyme.module.base.provider.mapper.SysMessageSendMapper;
import com.flyme.module.base.provider.service.SysMessageSendService;
import com.flyme.module.order.client.entity.Order;
import com.flyme.module.order.provider.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 消息发送接口实现
 *
 * @author flyme
 */
@Slf4j
@Service
@Transactional
public class SysMessageSendServiceImpl extends BaseServiceImpl<SysMessageSendMapper, SysMessageSend> implements SysMessageSendService {
    @Autowired
    private OrderService orderService;

    @Override
    public void deleteByMessageId(Long... messageId) {
        baseMapper.delete(q().in("messageId", messageId));
    }

    @Override
    public EntityMap getLastUserMessage(Long userId) {
        CriteriaQuery cq = new CriteriaQuery();
        cq.select("ms.id,ms.targetId,ms.createTime,ms.messageStatus,ms.messageType");
        cq.eq("userId", userId).ne("messageType", "1");
        cq.orderByDesc("createTime");
        cq.last("limit 1");
        EntityMap map = getEntityMap(cq);
        if(ObjectUtils.isNotEmpty(map)) {
            Order order = orderService.getById(map.get("targetId"));
            if (ObjectUtils.isNotEmpty(order)) {
                map.put("title", "订单支付成功提醒");
                map.put("orderNo", order.getOrderNo());
                map.put("messageTitle", order.getOrderTitle());
                map.put("payDate", order.getPayDate());
                map.put("orderId", order.getId());
            }
        }
        return map;
    }

    @Override
    public EntityMap getLastSysrMessage(Long userId) {
        CriteriaQuery cq = new CriteriaQuery();
        cq.select("message.id,ms.messageType,ms.createTime,message.messageTitle,message.messageContent,ms.messageStatus");
        cq.eq("ms.messageType", "1");
        cq.eq("userId", userId);
        cq.createAlias("message", "message");
        cq.orderByDesc("ms.createTime");
        cq.last("limit 1");
        return getEntityMap(cq);
    }



    /**
     * 根据类型统计消息未读条数
     */
    public int countByStatus(Long userId, String messageType) {
        return count(q().eq("userId", userId).eq("messageStatus", 0).eq("messageType", messageType));
    }


    /**
     * 查询用户订单如未读标记已读
     */
    public boolean updateMessage(Long userId, String targetId){
        SysMessageSend messageSend = getOne(q().eq(true,"userId", userId).eq(true, "targetId", targetId).eq(true, "messageType", 2).eq(true, "messageStatus", 0),false);
        if(ObjectUtils.isNotEmpty(messageSend)){
            messageSend.setMessageStatus(1);
            return saveOrUpdate(messageSend);
        }
        return false;
    }

    /**
     * 将用户所有订单消息更新为已读
     */
    public boolean updateByUser(Long userId, Integer type){
        UpdateWrapper u = new UpdateWrapper();
        return update(u.set(true, "messageStatus", 1).eq(true, "userId", userId).eq(true, "messageStatus", 0).eq(true, "messageType", type));
    }

    /**
     * 根据用户id查询消息列表
     */
    public List<String> selectByUser(Long userId){
        return listObjs(q().select("messageId").eq("userId", userId), e -> e.toString());
    }

}